<?php
// $Id: template.php,v 1.16.2.2 2009/08/10 11:32:54 goba Exp $

/**
 * Preprocessor for page.tpl.php template file.
 */
function phptemplate_preprocess_page(&$vars, $hook) {
  // For easy printing of variables.
  $vars['logo_img']         = $vars['logo'] ? theme('image', substr($vars['logo'], strlen(base_path())), t('Home'), t('Home')) : '';
  $vars['linked_logo_img']  = $vars['logo_img'] ?
    l($vars['logo_img'], '', array('rel' => 'home', 'title' => t('Home'), 'html' => TRUE)) :
    l('<img src="'. base_path() . path_to_theme() .'/images/logo.png" />', '<front>', array('rel' => 'home', 'title' => t('Home'), 'html' => TRUE));
  $vars['linked_logo_img'] = '<div class="site-logo">'. $vars['linked_logo_img'] .'</div>';

  $vars['main_menu'] = theme('links', $vars['primary_links'], array('class' => 'links menu', 'id' => 'primary-link'));
  $vars['secondary_menu'] = theme('links', $vars['secondary_links'], array('class' => 'links menu', 'id' => 'secondary-link'));

  if (module_exists('page_title')) {
    $vars['head_title'] = page_title_page_get_title();
  }
//  print_r($_GET);
  $vars['body_classes'] .= ' '. arg(0) .'-'. arg(1);
}

function phptemplate_preprocess_node(&$vars, $hook) {

}

function phptemplate_preprocess_views_view(&$vars, $hook) {

}

//=============================
//   Begin Browser Detector
//=============================
function phptemplate_get_special_styles() {
  $browser = '';
  $navigator_user_agent = ( isset( $_SERVER['HTTP_USER_AGENT'] ) ) ? strtolower( $_SERVER['HTTP_USER_AGENT'] ) : '';
  if (stristr($navigator_user_agent, "opera")) {
    $browser = 'opera';
    $dom_browser = true;
  }
  elseif (stristr($navigator_user_agent, "chrome")) {
    $browser = 'chrome';
    $dom_browser = true;
  }
  elseif ((stristr($navigator_user_agent, "konqueror")) OR (stristr($navigator_user_agent, "safari"))) {
    $browser = 'safari';
    $dom_browser = true;
  }
  else {
    $browser = false;
  }
//  print base_path() . path_to_theme() .'/css/'. $browser .'.css?' . time();
  if ($browser) {
    $special_css = '<link type="text/css" rel="stylesheet" href="'. base_path() . path_to_theme() .'/css/'. $browser .'.css?' . time() . '" media="screen, projection"/>';
    return $special_css;
  }
}

/**
 * Generates IE 6 CSS links.
 */
function phptemplate_get_ie_six_styles() {
  $iecss = '<link type="text/css" rel="stylesheet" href="'. base_path() . path_to_theme() .'/css/ie6.css?' . time() . '" media="screen, projection"/>';

  return $iecss;
}

/**
 * Generates IE 7 CSS links.
 */
function phptemplate_get_ie_seven_styles() {
  $iecss = '<link type="text/css" rel="stylesheet" href="'. base_path() . path_to_theme() .'/css/ie7.css?' . time() . '" media="screen, projection"/>';

  return $iecss;
}

/**
 * Generates IE 8 CSS links.
 */
function phptemplate_get_ie_eight_styles() {
  $iecss = '<link type="text/css" rel="stylesheet" href="'. base_path() . path_to_theme() .'/css/ie8.css?' . time() . '" media="screen, projection"/>';

  return $iecss;
}

/**
 * Contextually adds 960 Grid System classes.
 *
 * The first parameter passed is the *default class*. All other parameters must
 * be set in pairs like so: "$variable, 3". The variable can be anything available
 * within a template file and the integer is the width set for the adjacent box
 * containing that variable.
 *
 *  class="<?php print ns('grid-16', $var_a, 6); ?>"
 *
 * If $var_a contains data, the next parameter (integer) will be subtracted from
 * the default class. See the README.txt file.
 */
function ns() {
  $args = func_get_args();
  $default = array_shift($args);
  // Get the type of class, i.e., 'grid', 'pull', 'push', etc.
  // Also get the default unit for the type to be procesed and returned.
  list($type, $return_unit) = explode('-', $default);

  // Process the conditions.
  $flip_states = array('var' => 'int', 'int' => 'var');
  $state = 'var';
  $var_state = '';
  foreach ($args as $arg) {
    if ($state == 'var') {
      $var_state = !empty($arg);
    }
    elseif ($var_state) {
      $return_unit = $return_unit - $arg;
    }
    $state = $flip_states[$state];
  }

  $output = '';
  // Anything below a value of 1 is not needed.
  if ($return_unit > 0) {
    $output = $type . '-' . $return_unit;
  }
  return $output;
}
